<template>
  <div class="helplist">
    <div class="title">
      <div><h2>交流信息</h2></div>
      <div>
        <el-button type="text" icon="el-icon-plus" @click="$router.push('/admin/createhelp')">创建交流信息</el-button>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="createtime" label="日期">
          <template slot-scope="scope">
            {{scope.row.createtime|dataFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="help_title" label="标题"></el-table-column>
        <el-table-column prop="help_lable" label="分类"></el-table-column>
        <el-table-column prop="help_state" label="状态">
          <template slot-scope="scope">{{scope.row.ispublic|stateFilter}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="$router.push(`/admin/updatehelp/${scope.row.help_id}`) "
              type="text"
              size="small">编辑</el-button>
            <el-button type="text" size="small" @click="deleteHelp(scope.row.help_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20]"
        :page-size="pagelistquery.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagelistquery.total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'helplist',
  data () {
    return {
      pagelistquery: {
        total: 0,
        page: 1,
        pagesize: 10
      },
      tableData: []
    }
  },
  filters: {
    stateFilter (state) {
      if (state === 1) {
        return '审核通过'
      }
      if (state === 0) {
        return '正在审核'
      }
      if (state === -1) {
        return '审核未通过'
      }
    }
  },
  methods: {
    async deleteHelp (helpid) {
      const result = await this.$axios.post('/webadmin/deletehelp',
        this.qs.stringify({ help_id: helpid }))
      if (result.data.state.type === 'SUCCESS') {
        this.$message.success('删除成功')
        this.getHelpList()
      }
    },
    handleSizeChange (val) {
      this.pagelistquery.pagesize = val
      this.getHelpList()
    },
    handleCurrentChange (val) {
      this.pagelistquery.page = val
      this.getHelpList()
    },
    async getHelpList () {
      const data = {
        page: this.pagelistquery.page,
        pagesize: this.pagelistquery.pagesize
      }
      const result = await this.$axios.post('/webadmin/gethelplist', this.qs.stringify(data))
      if (result.data.state.type === 'SUCCESS') {
        this.tableData = result.data.data
        this.pagelistquery.total = result.data.count
        if (this.pagelistquery.total === 0) {
          this.tableData = []
        }
      }
    }
  },
  created () {
    this.getHelpList()
  }
}
</script>

<style scoped>

</style>
